package com.tulingxueyuan.mall.modules.pms.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.tulingxueyuan.mall.dto.ProductConditionDTO;
import com.tulingxueyuan.mall.dto.ProductPramaterDTO;
import com.tulingxueyuan.mall.dto.ProductUpdateInitDTO;
import com.tulingxueyuan.mall.modules.pms.model.*;
import com.tulingxueyuan.mall.modules.pms.mapper.PmsProductMapper;
import com.tulingxueyuan.mall.modules.pms.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.netty.util.internal.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.List;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author XuShu
 * @since 2023-05-08
 */
@Service
public class PmsProductServiceImpl extends ServiceImpl<PmsProductMapper, PmsProduct> implements PmsProductService {
    //   会员价格
    @Resource
    private PmsMemberPriceService pmsMemberPriceService;
    //   商品满减
    @Resource
    private PmsProductFullReductionService pmsProductFullReductionService;
    //   会员阶梯价格
    @Resource
    private PmsProductLadderService pmsProductLadderService;
    //   商品相关属性
    @Resource
    private PmsProductAttributeValueService pmsProductAttributeValueService;
    //   会员价格
    @Resource
    private PmsSkuStockService pmsSkuStockService;
    @Resource
    private PmsProductMapper pmsProductMapper;

    /**
     * 查询商品列表
     *
     * @param productConditionDTO
     * @return
     */
    @Override
    public Page listPage(ProductConditionDTO productConditionDTO) {
        Page<PmsProduct> pageInfo = new Page<>(productConditionDTO.getPageNum(),productConditionDTO.getPageSize());
        LambdaQueryWrapper<PmsProduct> wrapper = new LambdaQueryWrapper<>();
//        商品名称
        if (StrUtil.isNotEmpty(productConditionDTO.getKeyword())) {
            wrapper.like(PmsProduct::getName, productConditionDTO.getKeyword());
        }
        //        商品货号
        if (productConditionDTO.getProductSn() != null) {
            wrapper.eq(PmsProduct::getProductSn, productConditionDTO.getProductSn());
        }
        //        商品分类
        if (productConditionDTO.getProductCategoryId() != null) {
            wrapper.eq(PmsProduct::getProductCategoryId, productConditionDTO.getProductCategoryId());
        }
        //        商品品牌
        if (productConditionDTO.getBrandId() != null) {
            wrapper.eq(PmsProduct::getBrandId, productConditionDTO.getBrandId());
        }
        //        商品审核状态
        if (productConditionDTO.getVerifyStatus() != null) {
            wrapper.eq(PmsProduct::getVerifyStatus, productConditionDTO.getVerifyStatus());
        }
        //        商品上架状态
        if (productConditionDTO.getPublishStatus() != null) {
            wrapper.eq(PmsProduct::getPreviewStatus, productConditionDTO.getPublishStatus());
        }
        wrapper.orderByAsc(PmsProduct::getSort);
        this.page(pageInfo, wrapper);
        return pageInfo;
    }


    /**
     * 根据getPublishStatus修改相对应状态
     *
     * @param ids
     * @param publishStatus
     * @param getPublishStatus
     * @return
     */
    public boolean updateStatus(List<Integer> ids, Integer publishStatus, SFunction<PmsProduct, ?> getPublishStatus) {
        LambdaUpdateWrapper<PmsProduct> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(getPublishStatus, publishStatus)
                .in(PmsProduct::getId, ids);
        boolean update = this.update(wrapper);
        return update;
    }

    @Override
    @Transactional
    public boolean create(ProductPramaterDTO productPramaterDTO) {
        boolean b = false;
//        先添加商品  获取商品类
        PmsProduct pmsProduct = productPramaterDTO;
        if (pmsProduct != null) {
             b = this.save(pmsProduct);
//            在添加跟商品有有关实体,统一遍历给对象赋值product_id,并添加
            if (b) {
//                为了解决前端传来的其他促销
                switch (productPramaterDTO.getPromotionType()) {
                    case 2:
                        //                商品会员价格
                        List<PmsMemberPrice> memberPriceList = productPramaterDTO.getMemberPriceList();
                        savaProductIdWithAdd(memberPriceList, productPramaterDTO.getId(), pmsMemberPriceService);
                        break;
                    case 3:
                        //                商品阶梯价格
                        List<PmsProductLadder> productLadderList = productPramaterDTO.getProductLadderList();
                        savaProductIdWithAdd(productLadderList, productPramaterDTO.getId(), pmsProductLadderService);
                        break;
                    case 4:
                        //                商品满减价格
                        List<PmsProductFullReduction> productFullReductionList = productPramaterDTO.getProductFullReductionList();
                        savaProductIdWithAdd(productFullReductionList, productPramaterDTO.getId(), pmsProductFullReductionService);

                }

                //                商品属性
                List<PmsProductAttributeValue> productAttributeValueList = productPramaterDTO.getProductAttributeValueList();
                savaProductIdWithAdd(productAttributeValueList, productPramaterDTO.getId(), pmsProductAttributeValueService);
                //                商品会员价格
                List<PmsSkuStock> skuStockList = productPramaterDTO.getSkuStockList();
                savaProductIdWithAdd(skuStockList, productPramaterDTO.getId(), pmsSkuStockService);
            }
        }
        return b;
    }

    /**
     * 修改回显
     * @param id
     * @return
     */
    @Override
    public ProductUpdateInitDTO getUpdateInfo(Long id) {
        ProductUpdateInitDTO productUpdateInitDTO = pmsProductMapper.getInfo(id);
        return productUpdateInitDTO;
    }

    @Override
    @Transactional
    public boolean updateWithAll(ProductPramaterDTO productPramaterDTO) {
//        先删后加
        boolean b = false;
//        先添加商品  获取商品类
        PmsProduct pmsProduct = productPramaterDTO;
        if (pmsProduct != null) {
            this.removeById(productPramaterDTO.getId());
            b = this.save(pmsProduct);
//            在添加跟商品有有关实体,统一遍历给对象赋值product_id,并添加
            if (b) {
//                为了解决前端传来的其他促销
                switch (productPramaterDTO.getPromotionType()) {
                    case 2:
                        //                商品会员价格
                        List<PmsMemberPrice> memberPriceList = productPramaterDTO.getMemberPriceList();
                        removeProductIdWithAdd(productPramaterDTO.getId(),pmsMemberPriceService);
                        savaProductIdWithAdd(memberPriceList, productPramaterDTO.getId(), pmsMemberPriceService);
                        break;
                    case 3:
                        //                商品阶梯价格
                        List<PmsProductLadder> productLadderList = productPramaterDTO.getProductLadderList();
                        removeProductIdWithAdd(productPramaterDTO.getId(),pmsProductLadderService);
                        savaProductIdWithAdd(productLadderList, productPramaterDTO.getId(), pmsProductLadderService);
                        break;
                    case 4:
                        //                商品满减价格
                        List<PmsProductFullReduction> productFullReductionList = productPramaterDTO.getProductFullReductionList();
                        removeProductIdWithAdd(productPramaterDTO.getId(),pmsProductFullReductionService);
                        savaProductIdWithAdd(productFullReductionList, productPramaterDTO.getId(), pmsProductFullReductionService);

                }

                //                商品属性
                List<PmsProductAttributeValue> productAttributeValueList = productPramaterDTO.getProductAttributeValueList();
                removeProductIdWithAdd(productPramaterDTO.getId(),pmsProductAttributeValueService);
                savaProductIdWithAdd(productAttributeValueList, productPramaterDTO.getId(), pmsProductAttributeValueService);
                //                商品会员价格
                List<PmsSkuStock> skuStockList = productPramaterDTO.getSkuStockList();
                removeProductIdWithAdd(productPramaterDTO.getId(),pmsSkuStockService);
                savaProductIdWithAdd(skuStockList, productPramaterDTO.getId(), pmsSkuStockService);
            }
        }
        return b;
    }

    /**
     * 统一给商品关联表productid赋值并 新增
     * @param list
     * @param productId
     * @param iService
     */
    public void savaProductIdWithAdd(List list, Long productId, IService iService) {
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
//        循环反射赋值product——Id
        try {
            for (Object pojo : list) {
                Method method = pojo.getClass().getMethod("setProductId", Long.class);
                method.invoke(pojo, productId);
                Method setId = pojo.getClass().getMethod("setId", Long.class);
                setId.invoke(pojo, (Long)null);
            }
            iService.saveBatch(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void removeProductIdWithAdd(Long id,IService iService){
//        new LambdaUpdateWrapper<>()
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.eq("product_id",id);
        iService.remove(wrapper);
    }
}
